//
//  MeViewController.m
//  SXH
//
//  Created by 房 国生 on 15/6/25.
//  Copyright (c) 2015年 lyss. All rights reserved.
//

#import "MeViewController.h"
#import "MeTableViewCell.h"
#import "LoginViewController.h"
#import "FindMeViewController.h"
#import "UIButton+WebCache.h"
#import "MeProfileViewController.h"
#import "DownloadManageViewController.h"
#import "MessageViewController.h"
#import "OrderViewController.h"
#import "CartViewController.h"


#import "UserEntity.h"

@interface MeViewController ()

@property (nonatomic, strong) IBOutlet UIButton *btnHeader;
@property (nonatomic, strong) IBOutlet UIButton *btnUserName;
@property (strong, nonatomic) IBOutlet UIView *viewMe;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollViewMe;

@end

@implementation MeViewController
@synthesize tableViewMe, mutableArrayMe;
@synthesize btnUserName, btnHeader;@synthesize mainVC, wrapperMainVC;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"我的下载", @"name", @"登录_04.png", @"image", nil];
    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"我的游记", @"name", @"登录_07.png", @"image", nil];
    //NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:
    //                      @"我的足迹", @"name", @"登录_09.png", @"image", nil];
    NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"我的订单", @"name", @"订单.png", @"image", nil];
    
    mutableArrayMe = [[NSMutableArray alloc] init];
    
    [mutableArrayMe addObject:dic1];
    [mutableArrayMe addObject:dic2];
    [mutableArrayMe addObject:dic3];
    
    self.btnHeader.layer.masksToBounds = YES;
    self.btnHeader.layer.cornerRadius = self.btnHeader.frame.size.width*0.5;
    //    self.btnHeader.layer.borderWidth = 2.0;
    //    self.btnHeader.layer.borderColor = [UIColor colorWithRed:0.22 green:0.69 blue:0.9 alpha:1].CGColor;
    [self.scrollViewMe addSubview:self.viewMe];
    [self refreshView];
}

- (void)refreshView {
    UserEntity *userEntity = [UserEntity sharedInstance];
    
    if (userEntity.userAlbum != nil) {
        [btnHeader sd_setImageWithURL:[NSURL URLWithString:userEntity.userAlbum] forState:UIControlStateNormal];
    }
    
    if (userEntity.nickName != nil) {
        [btnUserName setTitle:userEntity.nickName forState:UIControlStateNormal];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [mutableArrayMe count];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *couponTableViewCellIdentifier=@"MeTableViewCell";
    MeTableViewCell *cell = (MeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:couponTableViewCellIdentifier];
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MeTableViewCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    NSDictionary *dic = [mutableArrayMe objectAtIndex:indexPath.row];
    cell.labelName.text = [dic objectForKey:@"name"];
    cell.imageViewIcon.image = [UIImage imageNamed:[dic objectForKey:@"image"]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 46;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3) {
        
        UserEntity *entity = [UserEntity sharedInstance];
        
        if (entity.login == YES) {
            switch (indexPath.row) {
                case 1:{
                    FindMeViewController *vc = [[FindMeViewController alloc] init];
                    vc.userId = entity.userId;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 2:{
                    UserEntity *entity = [UserEntity sharedInstance];
                    OrderViewController *vc = [[OrderViewController alloc] init];
                    
                    vc.userId = entity.userId;
                    if (self.mainVC != nil) {
                        vc.mainVC = self.mainVC;
                        [self.navigationController pushViewController:vc animated:YES];
                    } else {
                        vc.wrapperMainVC = self.wrapperMainVC;
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                }
                    break;
                case 3:{
                    CartViewController *vc = [[CartViewController alloc] init];
                    if (self.mainVC != nil) {
                        vc.mainVC = self.mainVC;
                        [self.navigationController pushViewController:vc animated:YES];
                    } else {
                        vc.wrapperMainVC = self.wrapperMainVC;
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                }
                    break;
                default:
                    break;  
            }
        } else {
            LoginViewController *vc = [[LoginViewController alloc] init];
            vc.meVC = self;
            if (mainVC == nil) {
                vc.wrapperMainVC = wrapperMainVC;
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                vc.mainVC = mainVC;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
    } else if (indexPath.row == 0) {
        DownloadManageViewController *vc = [[DownloadManageViewController alloc] init];
        if (self.mainVC != nil) {
            vc.mainVC = self.mainVC;
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            vc.wrapperMainVC = self.wrapperMainVC;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
}

- (IBAction)loginAction:(id)sender {
    
    UserEntity *userEntity = [UserEntity sharedInstance];
    if (userEntity.login == NO) {
        LoginViewController *vc = [[LoginViewController alloc] init];
        vc.meVC = self;
        
        if (mainVC == nil) {
            vc.wrapperMainVC = wrapperMainVC;
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            vc.mainVC = mainVC;
            [self.navigationController pushViewController:vc animated:YES];
        }
    } else {
        MeProfileViewController *vc = [[MeProfileViewController alloc] init];
        vc.meVC = self;
        if (mainVC == nil) {
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

- (IBAction)enterMessageVC:(id)sender {
    UserEntity *userEntity = [UserEntity sharedInstance];
    if (userEntity.login == NO) {
        LoginViewController *vc = [[LoginViewController alloc] init];
        vc.meVC = self;
        
        if (mainVC == nil) {
            vc.wrapperMainVC = wrapperMainVC;
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            vc.mainVC = mainVC;
            [self.navigationController pushViewController:vc animated:YES];
        }
    } else {
        MessageViewController *vc = [[MessageViewController alloc] init];
        if (mainVC == nil) {
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

- (void) pushMessageInfoVC:(BOOL)pushNotification {
    
    MessageViewController *vc = [[MessageViewController alloc] init];
    if (mainVC == nil) {
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"MeViewController"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"MeViewController"];
}
@end
